﻿using AutoMapper;
using BS.Domain.Slaughter;
using BS.Infra;
using BS.Infra.Interface;
using MediatR;

namespace BS.Write.Api.Handler.butchery
{
    public class AddSlaughterCommandHandler : IRequestHandler<InsertSlaughterCommand, ApiResult>
    {
        private readonly ISlaughtersRepository _slaughtersRepository;

        private readonly IMapper _mapper;

        private readonly ILogger<AddSlaughterCommandHandler> _logger;

        public AddSlaughterCommandHandler(ISlaughtersRepository slaughtersRepository, IMapper mapper, ILogger<AddSlaughterCommandHandler> logger)
        {
            _slaughtersRepository = slaughtersRepository;
            _mapper = mapper;
            _logger = logger;
        }
        public async Task<ApiResult> Handle(InsertSlaughterCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var slaughter = _mapper.Map<Slaughters>(request);
                var result= await _slaughtersRepository.InsertAsync(slaughter);
                return ApiResult.Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Insert slaughter failed.");
                return ApiResult.Fail(ex.Message);
            }
        }
    }
}
public class InsertSlaughterCommand:IRequest<ApiResult>
{
    /// <summary>
    /// 批次
    /// </summary>
    public string Batch { get; set; } = string.Empty;
    /// <summary>
    /// 到厂均重
    /// </summary>
    public int WeightToFactoty { get; set; }
    /// <summary>
    /// 到厂时间
    /// </summary>
    public DateTime ArrivalTime { get; set; }
    /// <summary>
    /// 屠宰时间
    /// </summary>
    public DateTime SlaughterTime { get; set; }
    /// <summary>
    /// 硕源ID
    /// </summary>
    public int TrId { get; set; }
}